<template>
  <el-tag 
    :type="tagType" 
    :effect="effect"
    :size="size"
    :class="['version-status-tag', `status-${status}`]"
    :style="tagStyle"
  >
    <el-icon v-if="showIcon" class="status-icon">
      <component :is="statusIcon" />
    </el-icon>
    {{ statusText }}
  </el-tag>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { 
  Clock, 
  Tools, 
  WarningFilled, 
  CircleCheckFilled, 
  SuccessFilled,
  FolderOpened
} from '@element-plus/icons-vue'
import type { VersionStatus } from '@/api/version'
import { useVersionStates } from '@/composables/useVersionStates'

interface Props {
  status: VersionStatus
  size?: 'large' | 'default' | 'small'
  effect?: 'dark' | 'light' | 'plain'
  showIcon?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  size: 'default',
  effect: 'light',
  showIcon: true
})

// 使用动态状态管理
const { getStateByCode, getStateName } = useVersionStates()

// 图标映射（根据状态名称关键词动态选择）
const getIconByStatusName = (statusName: string): any => {
  if (statusName.includes('发布') || statusName.includes('完成')) {
    return SuccessFilled
  }
  if (statusName.includes('测试') || statusName.includes('评审')) {
    return WarningFilled
  }
  if (statusName.includes('开发') || statusName.includes('进行')) {
    return Tools
  }
  if (statusName.includes('归档') || statusName.includes('废弃')) {
    return FolderOpened
  }
  if (statusName.includes('规划') || statusName.includes('草稿')) {
    return CircleCheckFilled
  }
  return Clock
}

// 将颜色代码映射到 Element Plus 的 tag type
const colorToTagType = (color: string): 'success' | 'info' | 'warning' | 'danger' | 'primary' => {
  const colorMap: Record<string, 'success' | 'info' | 'warning' | 'danger' | 'primary'> = {
    '#409EFF': 'primary',
    '#67C23A': 'success',
    '#E6A23C': 'warning',
    '#F56C6C': 'danger',
    '#909399': 'info'
  }
  
  // 尝试精确匹配
  if (colorMap[color]) {
    return colorMap[color]
  }
  
  // 尝试模糊匹配（根据颜色特征）
  if (color.includes('green') || color.includes('67c23a') || color.includes('success')) {
    return 'success'
  }
  if (color.includes('red') || color.includes('f56c6c') || color.includes('danger')) {
    return 'danger'
  }
  if (color.includes('orange') || color.includes('e6a23c') || color.includes('warning')) {
    return 'warning'
  }
  if (color.includes('blue') || color.includes('409eff') || color.includes('primary')) {
    return 'primary'
  }
  
  return 'info'
}

// 计算属性
const statusText = computed(() => {
  // 从动态状态管理系统获取状态名称
  return getStateName(props.status) || props.status
})

const tagType = computed((): 'success' | 'info' | 'warning' | 'danger' | 'primary' => {
  // 从动态状态管理系统获取状态颜色，转换为 tag type
  const state = getStateByCode(props.status)
  if (state?.color) {
    return colorToTagType(state.color)
  }
  
  // 如果状态管理系统没有颜色，根据状态名称判断
  if (state?.name) {
    const statusName = state.name.toLowerCase()
    if (statusName.includes('发布') || statusName.includes('完成') || statusName.includes('成功')) {
      return 'success'
    }
    if (statusName.includes('测试') || statusName.includes('评审') || statusName.includes('警告')) {
      return 'warning'
    }
    if (statusName.includes('开发') || statusName.includes('进行')) {
      return 'primary'
    }
    if (statusName.includes('废弃') || statusName.includes('失败') || statusName.includes('错误')) {
      return 'danger'
    }
  }
  
  return 'info'
})

const statusIcon = computed(() => {
  // 根据状态名称动态选择图标
  const state = getStateByCode(props.status)
  if (state?.name) {
    return getIconByStatusName(state.name)
  }
  return Clock
})

// 标签样式（如果动态状态有自定义颜色）
const tagStyle = computed(() => {
  const state = getStateByCode(props.status)
  if (state?.color) {
    // 使用动态颜色
    return {
      '--el-tag-text-color': state.color,
      '--el-tag-border-color': state.color + '40', // 添加透明度
      '--el-tag-bg-color': state.color + '10'
    }
  }
  return {}
})
</script>

<style scoped>
.version-status-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.status-icon {
  font-size: 12px;
}

/* 特殊状态样式 */
.status-planning {
  --el-tag-text-color: #606266;
  --el-tag-bg-color: #f4f4f5;
  --el-tag-border-color: #e4e7ed;
}

.status-development {
  --el-tag-text-color: #e6a23c;
  --el-tag-bg-color: #fdf6ec;
  --el-tag-border-color: #f5dab1;
}

.status-testing {
  --el-tag-text-color: #409eff;
  --el-tag-bg-color: #ecf5ff;
  --el-tag-border-color: #b3d8ff;
}

.status-pre_release {
  --el-tag-text-color: #e6a23c;
  --el-tag-bg-color: #fdf6ec;
  --el-tag-border-color: #f5dab1;
}

.status-released {
  --el-tag-text-color: #67c23a;
  --el-tag-bg-color: #f0f9ff;
  --el-tag-border-color: #c2e7b0;
}

.status-deprecated {
  --el-tag-text-color: #f56c6c;
  --el-tag-bg-color: #fef0f0;
  --el-tag-border-color: #fde2e2;
}

.status-archived {
  --el-tag-text-color: #909399;
  --el-tag-bg-color: #f4f4f5;
  --el-tag-border-color: #e4e7ed;
}
</style>